<?php

error_reporting ( E_ERROR );
ini_set ( 'display_errors', 1 );

class Controllers_memcache extends RestController {

  public function get() {
    try {
      global $memcache;

      if(@$_REQUEST['show'] == 1){    
        print_r($this->getMemcacheKeys());
       // echo '<hr />';
       // print_r($this->printDetails($memcache->getStats()));
        die;
      }

      echo 'session key' . session_id();
      echo '<hr />';
      echo 'session value: ' . $memcache->get( session_id() ) . '<br />';

      if (! empty ( $this->request ['params'] ['show'] )) {
        die ( $memcache->report () );
      } 

      // throw new SGameException ( - 999, 'ERROR', 'GET request is not support' );
    } catch ( Exception $se ) {
      $this->responseStatus = $se->getCode ();
      $this->response = $se->getMessage ();
    }
  }

  public function post() {
  }
  public function put() {
  }
  public function delete() {
  }
  public function authen() {
  }


  /**
  * Function to get all memcache keys
  * @author Manish Patel
  * @Created:  28-May-2010
  * @modified: 16-Jun-2011
  */
  function getMemcacheKeys() {

    $memcache = new Memcache;
    $memcache->connect('127.0.0.1', 11211) or die ("Could not connect to memcache server");

    $list = array();
    $allSlabs = $memcache->getExtendedStats('slabs');
    $items = $memcache->getExtendedStats('items');
    foreach($allSlabs as $server => $slabs) {
      foreach($slabs AS $slabId => $slabMeta) {
        $cdump = $memcache->getExtendedStats('cachedump',(int)$slabId);
        foreach($cdump AS $keys => $arrVal) {
          if (!is_array($arrVal)) continue;
          foreach($arrVal AS $k => $v) {                  
            // echo $k .'<br>';
             echo '<pre>';
            print_r( serialize( $memcache->get( $k )) );
            echo '</pre><hr />';
          }
        }
      }
    }  
  }//EO getMemcacheKeys()

  function printDetails($status){

    echo "<table border='1'>";

    echo "<tr><td>Memcache Server version:</td><td> ".$status ["version"]."</td></tr>";
    echo "<tr><td>Process id of this server process </td><td>".$status ["pid"]."</td></tr>";
    echo "<tr><td>Number of seconds this server has been running </td><td>".$status ["uptime"]."</td></tr>";
    echo "<tr><td>Accumulated user time for this process </td><td>".$status ["rusage_user"]." seconds</td></tr>";
    echo "<tr><td>Accumulated system time for this process </td><td>".$status ["rusage_system"]." seconds</td></tr>";
    echo "<tr><td>Total number of items stored by this server ever since it started </td><td>".$status ["total_items"]."</td></tr>";
    echo "<tr><td>Number of open connections </td><td>".$status ["curr_connections"]."</td></tr>";
    echo "<tr><td>Total number of connections opened since the server started running </td><td>".$status ["total_connections"]."</td></tr>";
    echo "<tr><td>Number of connection structures allocated by the server </td><td>".$status ["connection_structures"]."</td></tr>";
    echo "<tr><td>Cumulative number of retrieval requests </td><td>".$status ["cmd_get"]."</td></tr>";
    echo "<tr><td> Cumulative number of storage requests </td><td>".$status ["cmd_set"]."</td></tr>";

    $percCacheHit=((real)$status ["get_hits"]/ (real)$status ["cmd_get"] *100);
    $percCacheHit=round($percCacheHit,3);
    $percCacheMiss=100-$percCacheHit;

    echo "<tr><td>Number of keys that have been requested and found present </td><td>".$status ["get_hits"]." ($percCacheHit%)</td></tr>";
    echo "<tr><td>Number of items that have been requested and not found </td><td>".$status ["get_misses"]."($percCacheMiss%)</td></tr>";

    $MBRead= (real)$status["bytes_read"]/(1024*1024);

    echo "<tr><td>Total number of bytes read by this server from network </td><td>".$MBRead." Mega Bytes</td></tr>";
    $MBWrite=(real) $status["bytes_written"]/(1024*1024) ;
    echo "<tr><td>Total number of bytes sent by this server to network </td><td>".$MBWrite." Mega Bytes</td></tr>";
    $MBSize=(real) $status["limit_maxbytes"]/(1024*1024) ;
    echo "<tr><td>Number of bytes this server is allowed to use for storage.</td><td>".$MBSize." Mega Bytes</td></tr>";
    echo "<tr><td>Number of valid items removed from cache to free memory for new items.</td><td>".$status ["evictions"]."</td></tr>";

    echo "</table>";

  } 
} 